<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      
      <el-form-item label="联系人" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入联系人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入联系人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="接单人" prop="receiveName">
        <el-input
          v-model="queryParams.receiveName"
          placeholder="请输入接单人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接单电话" prop="receivePhone">
        <el-input
          v-model="queryParams.receivePhone"
          placeholder="请输入接单人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
      <el-col :span="1.5">
       
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['business:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">      
      <el-table-column label="订单编号" align="left" width="170" prop="num" >
        <template slot-scope="scope">
          <el-button
              type="text"
              @click="handleShow(scope.row)"
            >{{scope.row.num}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="订单分类" align="left" prop="categoryName"  />
      <el-table-column label="订单信息" align="left" prop="attribute" />
      <el-table-column label="下单人" align="left" prop="username" width="120" >
        <template slot-scope="scope">
          <div>{{scope.row.username}}</div>
          <div>{{scope.row.telephone}}</div>
        </template>
      </el-table-column>
      <el-table-column label="接单人" align="left" prop="receiveName" width="120" >
        <template slot-scope="scope">
          <div>{{scope.row.receiveName}}</div>
          <div>{{scope.row.receivePhone}}</div>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" align="left" prop="company" />
      <el-table-column label="所属代理" align="left" prop="proxy" />
     
      <el-table-column label="订单金额" align="left" prop="payable" >
        <template slot-scope="scope">
          <div>应付：￥{{scope.row.payable}}</div>
          <div>实付：￥{{scope.row.pay}}</div>
          <div>优惠：￥{{scope.row.payable - scope.row.pay}}</div>
        </template>
      </el-table-column>
      <el-table-column label="结算明细(分账对象,金额)" align="center" width="300" >
        <template slot-scope="scope">
            <el-table  :data="scope.row.amountList" v-if="scope.row.amountList.length > 0"  :show-header="false"
              >
              <el-table-column label="分账对象" align="center" prop="objectName" width="150" />
              <!-- <el-table-column label="比例" align="center" prop="rate" >
                <template slot-scope="scope">
                  {{Number(scope.row.rate) * 100 + '%'}}
                </template>
              </el-table-column> -->
              <el-table-column label="金额(元)" align="center" prop="fee" />              
            </el-table>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="orderTime" width="200">
        <template slot-scope="scope">
          <div>下单：{{ parseTime(scope.row.publishTime) }}</div>
          <div>接单：{{ parseTime(scope.row.receiveTime) }}</div>
          <div>预约：{{ parseTime(scope.row.orderTime) }}</div>
          <div>完成：{{ parseTime(scope.row.commentTime) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div style="text-align:left;">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-search"
              @click="handleShow(scope.row)"
            >详情</el-button>
           
          </div>          
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <show-order ref="showOrderForm" v-if="showOpen" ></show-order>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/business/order";

import ShowOrder from '../order/showOrder';

export default {
  name: "Order",
  components: {   
    ShowOrder
  },
  data() {
 
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      disabled: false,
      attrOpen: false,
      receiveOpen: false,
      addressOpen: false,
      distributeOpen: false,
      finishOpen: false,
      showOpen: false,
      // 预约时间时间范围
      daterangeOrderTime: [],
      // 创建人名称时间范围
      daterangeCreateTime: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        num: null,
        detail: null,
        orderTime: null,
        userId: null,
        username: null,
        telephone: null,
        receiveId: null,
        receiveName: null,
        receivePhone: null,
        receiveLimit: null,
        group: null,
        attribute: null,
        status: null,
        createName: null,
        createId: null,
        createTime: null,
        payable: null,
        pay: null,
        payMethod: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("tb_order_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      this.queryParams.status = 25;
      if (null != this.daterangeOrderTime && '' != this.daterangeOrderTime) {
        this.queryParams.params["beginOrderTime"] = this.daterangeOrderTime[0];
        this.queryParams.params["endOrderTime"] = this.daterangeOrderTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex < 3) {
        if (row.type == 1 ) {
          return {
            rowspan: 1,
            colspan: 1
          };
        } else if (row.type == 2) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else {
           return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        num: null,
        detail: null,
        orderTime: null,
        userId: null,
        username: null,
        telephone: null,
        receiveId: null,
        receiveName: null,
        receivePhone: null,
        receiveLimit: null,
        group: null,
        attribute: null,
        status: 0,
        remark: null,
        createName: null,
        createId: null,
        createTime: null,
        payable: null,
        pay: null,
        payMethod: null,
        address: null,
        addressId: null,
        orderAddress: {
          address: null
        }
      };
      this.resetForm("form");
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderTime = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
   
    handleShow(row){
      this.showOpen = true;
      this.$nextTick(() => {
        this.$refs['showOrderForm'].init(row);
      });
    },
   
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
